package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.utils.Tools;

/**
 * 滑动条控件
 * @author handinfo
 */
public class DWSlider extends DWControl
{
	private float m_length = 0;//表示总长度
	private float m_current = 0;//表示当前刻度
	private Bitmap m_sliver = null;
	private Bitmap m_tag = null;
	private int m_tag_w = 0;
	
	public DWSlider(Bitmap sliver, Bitmap tag, int lenght) {
		if (sliver == null || tag == null) {
			Tools.debugPrintln("滑动条图片为空!");
		}
		m_sliver = sliver;
		m_tag = tag;
		m_length = lenght;
		m_tag_w = tag.getWidth();
		setShowWideHigh(sliver.getWidth(), tag.getHeight());
	}
	
	public void render(DWGraphics g)
	{
		setClip(g);
		if (m_sliver != null) {
			g.drawBitmap(m_sliver, m_show_x, m_show_y + m_show_h / 2, Tools.LEFT_VCENTER);
		}
		if (m_tag != null) {
			g.drawBitmap(m_tag, m_show_x + m_deviant_x, m_show_y + m_show_h / 2, Tools.LEFT_VCENTER);
		}
	}
	
	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety) {
		setSubClip(g, x, y, w, h, offsetx, offsety);
		if (m_sliver != null) {
			g.drawBitmap(m_sliver, x + m_show_x - offsetx, y + m_show_y + m_show_h / 2 - offsety, Tools.LEFT_VCENTER);
		}
		if (m_tag != null) {
			g.drawBitmap(m_tag, x + m_show_x + m_deviant_x - offsetx, y + m_show_y + m_show_h / 2 - offsety, Tools.LEFT_VCENTER);
		}
	}

	public void logic()
	{
		
	}
	
	public void refreshControl() {
	}
	
	public float getCurrentValue() {
		return m_current;
	}
	
	public void setCurrentValue(float value) {
		m_current = value;
		float pct = m_current / m_length;
		m_deviant_x = (m_show_w - m_tag_w) * pct;
	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY) {
		if (checkPointOnRect(end.x, end.y)) {
			float x = end.x - m_show_x;
			m_deviant_x = x;
			if (m_deviant_x < 0) {
				m_deviant_x = 0;
			} else if (m_deviant_x > m_show_w - m_tag_w) {
				m_deviant_x = m_show_w - m_tag_w;
			}
			float pct = m_deviant_x / (m_show_w - m_tag_w);
			m_current = m_length * pct;
			if (m_listener != null) {
				m_listener.OnClick();
			}
		}
		return false;
	}
	
	public boolean doLongScroll(PointF start, PointF end, float distanceX, float distanceY) {
		if (checkPointOnRect(end.x, end.y)) {
			float x = end.x - m_show_x;
			m_deviant_x = x;
			if (m_deviant_x < 0) {
				m_deviant_x = 0;
			} else if (m_deviant_x > m_show_w - m_tag_w) {
				m_deviant_x = m_show_w - m_tag_w;
			}
			float pct = m_deviant_x / (m_show_w - m_tag_w);
			m_current = m_length * pct;
			if (m_listener != null) {
				m_listener.OnClick();
			}
		}
		return false;
	}

	public boolean doClick(float pointX, float pointY)
	{
		return false;
	}

}
